package com.weiqiao.api.rest;

import com.mingsoft.util.StringUtil;
import com.weiqiao.aop.log.Log;
import com.weiqiao.api.domain.ApiResponseEntity;
import com.weiqiao.api.domain.OrderApiEntity;
import com.weiqiao.api.domain.vo.OrderApiVO;
import com.weiqiao.api.domain.vo.OrderListApiVo;
import com.weiqiao.api.service.IOrderApiService;
import com.weiqiao.api.until.ApiResUtil;
import com.weiqiao.api.until.service.RedisApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ChenYF
 * @Description
 * @Date 2020/7/28 14:46
 */
@RestController
@RequestMapping(value = "mallApi/order/api")
public class OrderApiController {

    @Autowired
    private RedisApiService redisService;

    @Autowired
    private IOrderApiService orderService;

    @Log("信用卡下单接口")
    @PostMapping("/add-card-order")
    public ApiResponseEntity addCardOrder(@RequestBody @Validated OrderApiVO vo) {
        // 通过redis限制用户重复点击
        String redis_token = redisService.hget("orderToken:" + vo.getCompany_id(), vo.getUser_id() + ":" + vo.getToken());
        if (StringUtil.isBlank(redis_token)) {
            // order_公司id:userId:token
            redisService.hsetForDate("orderToken:" + vo.getCompany_id(), vo.getUser_id() + ":" + vo.getToken(), 1, 1800);
        } else {
            return ApiResUtil.buildRes(false, "不能重复下单");
        }
        // 订单状态
        if (!StringUtil.isInteger(vo.getOrder_type())) {
            return ApiResUtil.buildRes(false, "订单类型不能为空！");
        }
        ApiResponseEntity responseEntity =
                "0".equals(vo.getOrder_type()) ?
                        orderService.addCardOrder(vo) :
                        orderService.addIntegralOrder(vo);
        if (!responseEntity.isResult()) {
            redisService.hdelete("orderToken:" + vo.getCompany_id(), vo.getUser_id() + ":" + vo.getToken());
        }
        return responseEntity;
    }

    @Log("获取验证码")
    @GetMapping("/get-phone-code/{company_id}/{user_id}/{phone}")
    public ApiResponseEntity getPhoneCode(@PathVariable Integer company_id, @PathVariable Integer user_id, @PathVariable String phone) {
        if (!StringUtil.isInteger(company_id) || company_id != 4 || phone.length() != 11 || !StringUtil.isInteger(user_id)) {
            return ApiResUtil.buildRes(false, "参数错误", "");
        }
        String code = orderService.getPhoneCard(company_id, user_id, phone);
        return ApiResUtil.buildRes(StringUtil.isBlank(code) ? false : true, "验证码", code);
    }

    @Log("获取订单列表（信用卡/积分）")
    @GetMapping("order/order-list")
    public ApiResponseEntity getOrderList(@Validated OrderListApiVo vo) {
        List<OrderApiEntity> list = orderService.getOrderList(vo);
        return ApiResUtil.buildRes(true, ("0".equals(vo.getOrder_type()) ? "信用卡" : "积分兑换") + "订单", list);
    }
}
